<?php
namespace app\zhongcan\controller;
use app\model\NewModel;
use app\validate\NewValidate;
use cmf\controller\AdminBaseController;
use think\facade\Db;
use think\facade\View;

class NewController extends AdminBaseController{
    public function index(){
        $key=input('key','');
        $where=[];
        if($key){
            $where[]=['title|author','like',"%$key%"];
        }
        $list=Db::name('new')
            ->order('list_order','desc')
            ->where($where)
            ->paginate(1);
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function add(){
        return $this->fetch();
    }
    public function addPost(){
        //接收
        $data=input('post.');
        $conten1=htmlspecialchars_decode($data['content']);
        $conten2=cmf_replace_content_file_url($conten1,true);
        $content3=htmlspecialchars($conten2);
        $data['content']=$content3;
        $v=new NewValidate();
        $r=$v->scene('add')->check($data);
        if(!$r){
            $this->error($v->getError());
        }

        $model=NewModel::create($data);
        if($model){
            $this->success('添加成功','New/index');
        }else{
            $this->error('添加失败');
        }
    }
    public function edit(){
        $id = input('id');
        $new = Db::name('new')
            ->where('id', '=', $id)
            ->find();
        $content1=htmlspecialchars_decode($new['content']);
        $content2=cmf_replace_content_file_url($content1);
        $new['content']=$content2;
        $this->assign('new', $new);
        return $this->fetch();
    }
    public function editPost(){
        //接收
        $data=input('post.');
        $conten1=htmlspecialchars_decode($data['content']);
        $conten2=cmf_replace_content_file_url($conten1,true);
        $content3=htmlspecialchars($conten2);
        $data['content']=$content3;
        $v=new NewValidate();
        $r=$v->scene('edit')->check($data);
        if(!$r){
            $this->error($v->getError());
        }
        $model=NewModel::find($data['id']);

        $model->title=$data['title'];
        $model->image=$data['image'];
        $model->look_num=$data['look_num'];
        $model->author=$data['author'];
        $model->abstract=$data['abstract'];
        $model->content=$data['content'];
        if($model->save()){
            $this->success('保存成功','New/index');
        }else{
            $this->error('保存失败');
        }
    }
    public function del(){
        $id=input('id/d');
        $model=NewModel::destroy($id);
        if($model){
            $this->success('删除成功','New/index');
        }else{
            $this->error('删除失败');
        }
    }
    //排序
    public function px(){
        $model=new NewModel();
        parent::listOrders($model);
        $this->success('排序成功');
    }
    public function ylwz(){
        $id=input('id/d');
        $new=NewModel::find($id);
        $content = $new['content'];
        $content1 = htmlspecialchars_decode($content);
        $content2 = cmf_replace_content_file_url($content1, true);
        $new['content'] = $content2;
        $this->assign('new',$new);
        return $this->fetch('detail');
    }

    public function change()
    {
        $id = input('param.id', 0, 'intval'); // 获取传入的新闻 ID
        $new = NewModel::find($id);

        if ($new) {
            // 切换状态
            $new->switch = $new->switch == 1 ? 0 : 1;

            if ($new->save()) {
                $this->success('状态切换成功', 'New/index'); // 成功后重定向到新闻列表页
            } else {
                $this->error('状态切换失败', 'New/index'); // 失败后也重定向到新闻列表页
            }
        } else {
            $this->error('新闻不存在', 'New/index'); // 新闻不存在时重定向到新闻列表页
        }
    }
}